package com.sll.hosptials.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sll.hosptials.controller.req.DictInfoPageReq;
import com.sll.hosptials.controller.req.DictReq;
import com.sll.hosptials.model.DictInfo;
import com.sll.hosptials.model.DictType;
import com.sll.hosptials.service.DictInfoService;
import com.sll.hosptials.mapper.DictInfoMapper;
import com.sll.hosptials.service.DictTypeService;
import com.sll.hosptials.vo.R;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

import static com.sll.hosptials.config.ParamConfig.*;

/**
 * @author shaolulu
 * @description 针对表【dict_info(数据字典)】的数据库操作Service实现
 * @createDate 2024-01-04 15:46:55
 */
@Service
public class DictInfoServiceImpl extends ServiceImpl<DictInfoMapper, DictInfo> implements DictInfoService {
    @Resource
    private DictTypeService dictTypeService;

    private static String ROOT_DICT_ID = "-1";

    @Override
    public Page<DictInfo> page(DictInfoPageReq req) {
        LambdaQueryWrapper<DictInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNoneBlank(req.getDictCode()), DictInfo::getDictCode, req.getDictCode())
                .like(StringUtils.isNoneBlank(req.getDictName()), DictInfo::getDictName, req.getDictName())
                .like(StringUtils.isNoneBlank(req.getDictTypeCode()), DictInfo::getDictTypeCode, req.getDictTypeCode())
                .orderByDesc(DictInfo::getDictSort, DictInfo::getCreateDateTime);
        Page<DictInfo> page = this.page(new Page<>(req.getPageNo(), req.getPageSize()), queryWrapper);
//        处理，根据字典名查询以后，返回该数据（从根节点开始到当前节点结束）
        List<DictInfo> result = CollectionUtil.newArrayList();
        List<DictInfo> records = page.getRecords();
        System.out.println(records);
        for (DictInfo record : page.getRecords()) {
//            获取根节点
            DictInfo parentDict = getParentDict(record);
            result.add(parentDict);
        }
        page.setRecords(result);
        return page;
    }

    @Override
    public List<DictInfo> dictTreeList(DictReq dictReq) {
        LambdaQueryWrapper<DictInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(dictReq.getDictTypeCode()), DictInfo::getDictTypeCode, dictReq.getDictTypeCode());
//        queryWrapper.eq(StrUtil.isNotBlank(dictReq.getDictCode()), DictInfo::getDictCode, dictReq.getDictCode());
//        queryWrapper.like(StrUtil.isNotBlank(dictReq.getDictName()), DictInfo::getDictName, dictReq.getDictName());
//        如果传入dictTypeId，需翻译dictTypeCode
        if (Objects.nonNull(dictReq.getDictTypeId())) {
            DictType dictType = dictTypeService.getById(dictReq.getDictTypeId());
            queryWrapper.eq(DictInfo::getDictTypeCode, dictType.getDictTypeCode());
        }
        List<DictInfo> dictInfoList = this.list(queryWrapper);
//        处理树形结构，获取子集结构
        List<DictInfo> dictChild = getDictChild(dictInfoList);
//        以上会查询出子级树形结构，需去除，只保存无根节点的数据
        List<DictInfo> resultDict = CollectionUtil.newArrayList();
        for (DictInfo dictInfo : dictChild) {
            if (String.valueOf(dictInfo.getParentId()).equals(ROOT_DICT_ID)) {
                resultDict.add(dictInfo);
            }
        }
        return resultDict;
    }

    /**
     * 求子节点形成的树结构
     * @param dictInfoList
     * @return
     */
    private List<DictInfo> getDictChild(List<DictInfo> dictInfoList) {
        if (CollectionUtil.isNotEmpty(dictInfoList)) {
            for (DictInfo dictInfo : dictInfoList) {
                LambdaQueryWrapper<DictInfo> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(DictInfo::getParentId, dictInfo.getDictId());
                List<DictInfo> childDichildList = this.list(queryWrapper);
                dictInfo.setChildDict(childDichildList);
//                递归判断，子集是否还有下级节点
                getDictChild(childDichildList);
            }
        }
        return dictInfoList;
    }

    /**
     * 求当前节点的根节点
     * @param currentDictInfo
     * @return
     */
    private DictInfo getParentDict(DictInfo currentDictInfo) {
        if (!String.valueOf(currentDictInfo.getParentId()).equals(ROOT_DICT_ID)) {
            DictInfo dictInfo = this.getById(currentDictInfo.getParentId());
            currentDictInfo.setChildDict(ListUtil.toList(ObjUtil.clone(currentDictInfo)));
            currentDictInfo.setDictId(dictInfo.getDictId());
            currentDictInfo.setParentId(dictInfo.getParentId());
            currentDictInfo.setDictCode(dictInfo.getDictCode());
            currentDictInfo.setDictName(dictInfo.getDictName());
            currentDictInfo.setCreateDateTime(dictInfo.getCreateDateTime());
            getParentDict(currentDictInfo);
        }
        return currentDictInfo;
    }




}




